//
//  HealthTimeSetViewController.m
//  project
//
//  Created by 圆圆陈 on 2018/3/17.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "HealthTimeSetViewController.h"

@interface HealthTimeSetViewController ()
@property(nonatomic,strong)NSMutableArray *timeBtnArr;
@property(nonatomic,strong)NSMutableArray *weekBtnArr;
@end

@implementation HealthTimeSetViewController
{
    UIDatePicker *startPicker;
    UIDatePicker *endPicker;
    UILabel *lable;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _timeBtnArr = [NSMutableArray array];
    _weekBtnArr = [NSMutableArray array];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20),screenWidth, 44)];
    lable.textColor=[UIColor blackColor];
    lable.font=SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = _type==0?SN_Text(@"喝水提醒"):SN_Text(@"久坐提醒");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UIButton *rightButton = [[UIButton alloc] init];
    rightButton.frame = CGRectMake(HHHWIDTH-55-10, 20+FitSimpleIpx(0, 20), 55, 44);
    [rightButton setTitleNormal:HHH_Text(@"保存")];
    rightButton.titleLabel.font = SNFONT(17);
    [rightButton setTitleColorNormal:SNCOLORBLACK];
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 这里是用大写的 H
    NSString* dateStr = [formatter stringFromDate:[NSDate date]];
    NSDate *date = [formatter dateFromString:dateStr];
    
    startPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, naView.bottom+2+FitH(44), HHHWIDTH/2,FitH(150))];
    startPicker.datePickerMode = UIDatePickerModeTime;
    startPicker.backgroundColor = [UIColor clearColor];
    startPicker.tag = 100;
    startPicker.date = date;
//    startPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
    startPicker.locale = [NSLocale currentLocale];
    [startPicker addTarget:self
                    action:@selector(pickerChanged:)
          forControlEvents:UIControlEventValueChanged];
    [startPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
    [self.view addSubview:startPicker];
    
    NSArray *arr1 = @[@"",@"--",@""];
    for (int i=0; i<arr1.count; i++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, FitW(20), FitW(20))];
        label.center = CGPointMake(HHHWIDTH/4*(i+1), startPicker.centerY);
        label.text = arr1[i];
        label.textColor =SNCOLORBLACK;
        [self.view addSubview:label];
    }

    UIImageView *imageL = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 3, startPicker.height/3-3)];
    imageL.center = CGPointMake(1, startPicker.centerY);
    [imageL setImage:[UIImage imageWithColor:[UIColor blackColor]]];
    [self.view addSubview:imageL];
    
    UIImageView *imageR = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 3, startPicker.height/3-3)];
    imageR.center = CGPointMake(HHHWIDTH-1, startPicker.centerY);
    [imageR setImage:[UIImage imageWithColor:[UIColor blackColor]]];
    [self.view addSubview:imageR];
    
    endPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(HHHWIDTH/2, naView.bottom+2+FitH(44), HHHWIDTH/2,FitH(150))];
    endPicker.datePickerMode = UIDatePickerModeTime;
    endPicker.backgroundColor = [UIColor clearColor];
    endPicker.tag = 101;
    endPicker.date = date;
//    endPicker.locale = [NSLocale localeWithLocaleIdentifier:@"en_GB"];
    endPicker.locale = [NSLocale currentLocale];
    [endPicker addTarget:self
                  action:@selector(pickerChanged:)
        forControlEvents:UIControlEventValueChanged];
    [endPicker setValue:SNCOLORBLACK forKey:@"textColor"]; //设置字体颜色
    [self.view addSubview:endPicker];
    
    NSString *tmpStr = [dateStr substringWithRange:NSMakeRange(0, 11)];
    if (_type==0) {
        NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.drinkStartHour1,SN_USER.drinkStartMins1];
        startPicker.date =[formatter dateFromString:startStr];
        
        NSString *endStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.drinkEndHour1,SN_USER.drinkEndMins1];
        endPicker.date =[formatter dateFromString:endStr];
        endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
    }else{
        NSString *startStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.sedentaryStartHour1,SN_USER.sedentaryStartMins1];
        startPicker.date =[formatter dateFromString:startStr];
        
        NSString *endStr = [NSString stringWithFormat:@"%@%02ld:%02ld:00",tmpStr,SN_USER.sedentaryEndHour1,SN_USER.sedentaryEndMins1];
        endPicker.date =[formatter dateFromString:endStr];
        endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
    }
    
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, startPicker.bottom+FitH(12), FitW(100), FitH(26))];
    timeLabel.textColor = SNCOLORBLACK;
    timeLabel.text = HHH_Text(@"时间间隔");
    timeLabel.textAlignment = NSTextAlignmentCenter;
    timeLabel.font = [UIFont systemFontOfSize:FitFont(17)];
    [self.view addSubview:timeLabel];
    
    NSArray *arr = @[@"半小时",@"1小时",@"2小时",@"3小时"];
    CGFloat width = (HHHWIDTH-timeLabel.right-FitW(10))/arr.count;
    for (int i=0; i<arr.count; i++) {
        UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        timeBtn.frame = CGRectMake(timeLabel.right+FitW(5)+width*i,startPicker.bottom+FitH(10), width, FitH(30));
        timeBtn.tag = 100+i;
        [timeBtn setTitleColorNormal:SNCOLORBLACK];
        [timeBtn setTitleNormal:SN_Text(arr[i])];
        [timeBtn setImageNormal:@"未选中"];
        [timeBtn setImageSelecte:@"选中1"];
        timeBtn.titleLabel.font = [UIFont systemFontOfSize:FitFont(16)];
        [timeBtn addTouchUpTarget:self action:@selector(timeBtnClick:)];
        [self.view addSubview:timeBtn];
        [_timeBtnArr addObject:timeBtn];

        if (_type==0) {
            if (timeBtn.tag==100+(SN_USER.drinkinterval/30-1)) {
                timeBtn.selected = YES;
            }
        }else{
            if (timeBtn.tag==100+(SN_USER.sedentaryinterval/30-1)) {
                timeBtn.selected = YES;
            }
        }
    }
    NSArray *weekArr = @[HHH_Text(@"星期天"),HHH_Text(@"星期一"),HHH_Text(@"星期二"),HHH_Text(@"星期三"),HHH_Text(@"星期四"),HHH_Text(@"星期五"),HHH_Text(@"星期六")];
    CGFloat height =(HHHHEIGHT-timeLabel.bottom)/9;
    for (int i=0; i<weekArr.count; i++) {
        UILabel *weekLabel = [[UILabel alloc]initWithFrame:CGRectMake(FitW(25),-30+ timeLabel.bottom+height*(i+1)+(height-FitW(50))/2, HHHWIDTH/2, FitW(50))];
        weekLabel.text = weekArr[i];
        weekLabel.font = SNFONTMIN(15.0);
        weekLabel.textColor = SNCOLORBLACK;
        [self.view addSubview:weekLabel];
        
        UIButton *weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         weekBtn.frame = CGRectMake(HHHWIDTH-FitW(75),-30+ timeLabel.bottom+height*(i+1)+(height-FitW(50))/2, FitW(50), FitW(50));
        weekBtn.tag = 100+i;
        [weekBtn setImageNormal:@"未勾选"];
        [weekBtn setImageSelecte:@"已勾选"];
        [weekBtn addTouchUpTarget:self action:@selector(weekBtnClick:)];
        [self.view addSubview:weekBtn];
        
//        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, timeLabel.bottom+height*(i+2)-1, HHHWIDTH, 1)];
//        line.backgroundColor = SNCOLORGRAYLINE;
//        [self.view addSubview:line];
        if (_type==0) {
            switch (weekBtn.tag) {
                case 100:
                    weekBtn.selected = SN_USER.drinkSUN ;
                    break;
                case 101:
                    weekBtn.selected = SN_USER.drinkMON ;
                    break;
                case 102:
                    weekBtn.selected = SN_USER.drinkTUE ;
                    break;
                case 103:
                    weekBtn.selected = SN_USER.drinkWED ;
                    break;
                case 104:
                    weekBtn.selected = SN_USER.drinkTHU ;
                    break;
                case 105:
                    weekBtn.selected = SN_USER.drinkFRI ;
                    break;
                case 106:
                    weekBtn.selected = SN_USER.drinkSAT ;
                    break;
                default:
                    break;
            }
        }else{
            switch (weekBtn.tag) {
                case 100:
                    weekBtn.selected = SN_USER.sedentarySUN ;
                    break;
                case 101:
                    weekBtn.selected = SN_USER.sedentaryMON ;
                    break;
                case 102:
                    weekBtn.selected = SN_USER.sedentaryTUE ;
                    break;
                case 103:
                    weekBtn.selected = SN_USER.sedentaryWED ;
                    break;
                case 104:
                    weekBtn.selected = SN_USER.sedentaryTHU ;
                    break;
                case 105:
                    weekBtn.selected = SN_USER.sedentaryFRI ;
                    break;
                case 106:
                    weekBtn.selected = SN_USER.sedentarySAT ;
                    break;
                default:
                    break;
            }
        }
    }
    
    SNShadowView *shadowLine = [[SNShadowView alloc] init];
    shadowLine.frame = SNFRAME(0,20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
    [shadowLine showWithType:1];
    [self.view addSubview:shadowLine];
}

- (void)leftbuttonClick
{
    SN_USER = [SNUserInfoModel getUserInfoFromDBWithUserID:[KK_LastUser getObjectValue]];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightButtonClick{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString *start = [formatter stringFromDate:startPicker.date];
    NSString *end = [formatter stringFromDate:endPicker.date];
    
    if (_type==0) {
        SN_USER.drinkStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.drinkStartMins1  =[start substringWithRange:NSMakeRange(3, 2)].integerValue;
        SN_USER.drinkEndHour1 = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.drinkEndMins1  =[end substringWithRange:NSMakeRange(3, 2)].integerValue;
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                SN_USER.drinkOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startDrinkWater];
    }else{
        SN_USER.sedentaryStartHour1 = [start substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.sedentaryStartMins1  =[start substringWithRange:NSMakeRange(3, 2)].integerValue;
        SN_USER.sedentaryEndHour1 = [end substringWithRange:NSMakeRange(0, 2)].integerValue;
        SN_USER.sedentaryEndMins1 =[end substringWithRange:NSMakeRange(3, 2)].integerValue;
        for (UIButton *weekBtn in _weekBtnArr) {
            if (weekBtn.selected) {
                SN_USER.sedentaryOnce = NO;
            }
        }
        [SN_EveryDayModelHelp startsedentary];
    }
    [SN_USER updateToDB];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setType:(NSInteger)type{
    _type = type;
}
-(void)timeBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        for (UIButton *btn in _timeBtnArr) {
            if (btn.tag!=sender.tag) {
                btn.selected = !sender.selected;
            }
        }
        if (_type==0) {
            SN_USER.drinkinterval = 30*(sender.tag-100+1);
        }else{
            SN_USER.sedentaryinterval =30*(sender.tag-100+1);
        }
    }
}
-(void)pickerChanged:(UIDatePicker *)picker{
    endPicker.minimumDate = [startPicker.date dateByAddingTimeInterval:1800];
}
-(void)weekBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [_weekBtnArr addObject:sender];
    }else{
        [_weekBtnArr removeObject:sender];
    }
    if (_type==0) {
        if (sender.selected) {
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = YES;
                    break;
                case 101:
                    SN_USER.drinkMON = YES;
                    break;
                case 102:
                    SN_USER.drinkTUE = YES;
                    break;
                case 103:
                    SN_USER.drinkWED = YES;
                    break;
                case 104:
                    SN_USER.drinkTHU = YES;
                    break;
                case 105:
                    SN_USER.drinkFRI = YES;
                    break;
                case 106:
                    SN_USER.drinkSAT = YES;
                    break;
                default:
                    break;
            }
        }else{
            switch (sender.tag) {
                case 100:
                    SN_USER.drinkSUN = NO;
                    break;
                case 101:
                    SN_USER.drinkMON = NO;
                    break;
                case 102:
                    SN_USER.drinkTUE = NO;
                    break;
                case 103:
                    SN_USER.drinkWED = NO;
                    break;
                case 104:
                    SN_USER.drinkTHU = NO;
                    break;
                case 105:
                    SN_USER.drinkFRI = NO;
                    break;
                case 106:
                    SN_USER.drinkSAT = NO;
                    break;
                default:
                    break;
            }
        }
    }
    else{
        if (sender.selected){
            switch (sender.tag) {
                case 100:
                    SN_USER.sedentarySUN = YES;
                    break;
                case 101:
                    SN_USER.sedentaryMON = YES;
                    break;
                case 102:
                    SN_USER.sedentaryTUE = YES;
                    break;
                case 103:
                    SN_USER.sedentaryWED = YES;
                    break;
                case 104:
                    SN_USER.sedentaryTHU = YES;
                    break;
                case 105:
                    SN_USER.sedentaryFRI = YES;
                    break;
                case 106:
                    SN_USER.sedentarySAT = YES;
                    break;
                default:
                    break;
            }
        }else{
            switch (sender.tag) {
                case 100:
                    SN_USER.sedentarySUN = NO;
                    break;
                case 101:
                    SN_USER.sedentaryMON = NO;
                    break;
                case 102:
                    SN_USER.sedentaryTUE = NO;
                    break;
                case 103:
                    SN_USER.sedentaryWED = NO;
                    break;
                case 104:
                    SN_USER.sedentaryTHU = NO;
                    break;
                case 105:
                    SN_USER.sedentaryFRI = NO;
                    break;
                case 106:
                    SN_USER.sedentarySAT = NO;
                    break;
                default:
                    break;
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
